package com.hs.mobile.controller;

import java.util.List;

import org.apache.log4j.Logger; // for debugging
import com.hs.mobile.model.Category;
import com.hs.mobile.service.CategoryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;

/**
 * 
 * @author NAM
 *
 */
@Controller
public class CategoryController {
	protected static Logger logger = Logger.getLogger("categoryController");
	
	@Resource(name="categoryService")
	private CategoryService categoryService;
		 
	@Autowired
	public CategoryController(CategoryService categoryService) {
	  this.categoryService = categoryService;
	}
	 	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@GET
	@RequestMapping(value = "/category/{id}", method = RequestMethod.GET)
	@Produces(MediaType.APPLICATION_JSON)
	public @ResponseBody Category getCategory(@PathVariable("id") Long id) {
		logger.debug("Provider has received request to get category with id: " + id);		
		// Call service here
		return categoryService.findCategoryById(id);
	}
	
	/**
	 * 
	 * @return listCategory
	 */
	@GET
	@RequestMapping(value = "/categories",method = RequestMethod.GET)
	@Produces(MediaType.APPLICATION_JSON)
	public @ResponseBody List<Category> getCategory() {
		logger.debug("Provider has received request to get all categories");
		// Call service here. access database
		List<Category> listCategory = null;
		listCategory = categoryService.getAllCategories();		
		return listCategory;
	}	
	/**
	 * 
	 * @param category
	 */
	@RequestMapping(value = "/addcategory", method = RequestMethod.POST)
	@Produces(MediaType.APPLICATION_JSON)
	public @ResponseBody void addCategory(@RequestBody Category category) {
		 logger.debug("Provider has received request to add new category");
		 // Call service to here
		 categoryService.save(category);
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deletecategory/{id}", method = RequestMethod.DELETE)
	@Produces(MediaType.APPLICATION_JSON)
	public @ResponseBody List<Category> deleteCategory(@PathVariable("id") Long id) {
		logger.debug("Provider has received request to delete category with id: " + id);		
		// Call service here
		categoryService.delete(id);
		return categoryService.getAllCategories();
	}
	
	/**
	 * 
	 * @param id
	 */
	@PUT
	@RequestMapping(value="/updatecategory/{id}", method = RequestMethod.PUT)
	@Produces(MediaType.APPLICATION_JSON)
	public @ResponseBody void updateCategory(@PathVariable("id") Long id )
	{
		logger.debug("Provider has received request to update category with id :" + id );
		Category category = categoryService.findCategoryById(id);
		categoryService.update(category);
	}
}
